package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.EvaluateMapper;
import com.csr.csrcheck.pojo.Evaluate;
import com.csr.csrcheck.service.EvaluateService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.service.ex.EvaluateException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Resource
    private EvaluateMapper evaluateMapper;

    public List<Evaluate> getEvaluatelist() {
        List<Evaluate> list = this.evaluateMapper.getEvaluatelist();
        if (list == null)
            throw new EvaluateException("");
        return list;
    }

    public PageResult getlistpage(int pageNum, int pageSize, String company_name, String product_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(pageNum, pageSize, company_name, product_name));
    }

    public int addevlaute(Evaluate evaluate) {
        int row = this.evaluateMapper.addevlaute(evaluate);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int updateevlaute(Evaluate evaluate) {
        int row = this.evaluateMapper.updateevlaute(evaluate);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int deleteevlaute(int id) {
        int row = this.evaluateMapper.deleteevlaute(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Evaluate findevluateByid(int id) {
        return this.evaluateMapper.findevluateByid(id);
    }

    private PageInfo<Evaluate> getPageInfo(int pageNum, int pageSize, String company_name, String product_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Evaluate> list = this.evaluateMapper.getEvaluatepage(company_name, product_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}
